package com.example.ecmftlutil;


import java.util.ArrayList;
import java.util.List;

public class HierarchicalTablePrinter {

    public static void printTable(List<HierarchicalItem> items, int rootId) {
        printTable(items, rootId, 0);
    }

    private static void printTable(List<HierarchicalItem> items, int parentId, int level) {
        List<HierarchicalItem> children = getChildren(items, parentId);
        for (int i = 0; i < level; i++) {
            System.out.print("--");
        }
        for (HierarchicalItem item : children) {
            System.out.println(item);
            printTable(items, item.id, level + 1);
        }
    }

    private static List<HierarchicalItem> getChildren(List<HierarchicalItem> items, int parentId) {
        List<HierarchicalItem> result = new ArrayList<>();
        for (HierarchicalItem item : items) {
            if (item.parentId == parentId) {
                result.add(item);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<HierarchicalItem> items = new ArrayList<>();
        items.add(new HierarchicalItem(1, 0, "Item 1"));
        items.add(new HierarchicalItem(2, 0, "Item 2"));
        items.add(new HierarchicalItem(3, 1, "Item 1.1"));
        items.add(new HierarchicalItem(4, 1, "Item 1.2"));
        items.add(new HierarchicalItem(5, 2, "Item 2.1"));
        items.add(new HierarchicalItem(6, 3, "Item 1.1.1"));

        printTable(items, 0);
    }
}